package com.example.wuqujingdianphone.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.example.wuqujingdianphone.R;
import com.example.wuqujingdianphone.adapter.TTFenleiAdapter;
import com.example.wuqujingdianphone.api.ApiConfig;
import com.example.wuqujingdianphone.base.BaseActivity;
import com.example.wuqujingdianphone.bean.ABouFenleiBean;
import com.example.wuqujingdianphone.utils.AppManager;
import com.example.wuqujingdianphone.utils.GsonUtil;
import com.example.wuqujingdianphone.utils.NetWorkHelper;
import com.example.wuqujingdianphone.utils.ToastUtil;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Response;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.constant.SpinnerStyle;
import com.scwang.smartrefresh.layout.footer.ClassicsFooter;
import com.scwang.smartrefresh.layout.header.ClassicsHeader;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 视频列表
 */
public class TuShuFenLeiListActivity extends BaseActivity implements OnRefreshLoadmoreListener {

    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.title)
    TextView mTitle;
    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;
    private List<ABouFenleiBean.ListBean> listBeans = new ArrayList<>();
    private String title = "";
    private String id = "";

    private TTFenleiAdapter mAdapter;
    private int page = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_video_list);
        ButterKnife.bind(this);
        id = getIntent().getStringExtra("id");
        title = getIntent().getStringExtra("title");
        mTitle.setText(title);
        mToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        initView();
        initData(page);
    }

    private void initView() {
        mRecyclerView.setLayoutManager(new GridLayoutManager(getApplicationContext(), 3));
        mAdapter = new TTFenleiAdapter(getApplicationContext(), listBeans);
        mAdapter.openLoadAnimation();
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Intent intent = new Intent(getApplicationContext(), BookDetailActivity.class);
                intent.putExtra("bookId", listBeans.get(position).getId());
                intent.putExtra("bookTitle", listBeans.get(position).getNews_tit());
                startActivity(intent);
            }
        });
        //refresh
        refreshLayout.setEnableFooterFollowWhenLoadFinished(false);
        refreshLayout.setEnableAutoLoadmore(false);
        refreshLayout.setEnableScrollContentWhenLoaded(false);
        refreshLayout.setRefreshHeader(new ClassicsHeader(getApplicationContext()));
        refreshLayout.setRefreshFooter(new ClassicsFooter(getApplicationContext()).setSpinnerStyle(SpinnerStyle.Translate).setTextSizeTitle(14));
        refreshLayout.setOnRefreshLoadmoreListener(this);
    }

    private void initData(final int mPage) {
        showLoading(this);
        OkGo.<String>get(ApiConfig.APP_URL + "index.php/jiekou/getBookByPage")
                .tag(this)
                .params("lm", id)
                .params("pageIndex", String.valueOf(mPage))
                .params("pageSize", "20")
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(Response<String> response) {
                        String ds = response.body();
                        cancelLoading();
                        if (mPage == 1) {
                            listBeans.clear();
                        }
                        ABouFenleiBean bean = GsonUtil.GsonToBean(response.body(), ABouFenleiBean.class);
                        listBeans.addAll(bean.getList());
//                        mAdapter.setNewData(listBeans);
                        mAdapter.notifyDataSetChanged();
                    }

                    @Override
                    public void onError(Response<String> response) {
                        super.onError(response);
                        cancelLoading();
                        ToastUtil.show("加载失败");
                    }
                });
    }


    @Override
    public void onRefresh(final RefreshLayout refreshlayout) {
        refreshLayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (NetWorkHelper.isConnected(getApplicationContext())) {
                    page = 1;
                    initData(page);
                    refreshLayout.finishRefresh();
                    refreshLayout.resetNoMoreData();//恢复上拉状态
                } else {
                    refreshLayout.finishRefresh(false);
                    refreshLayout.resetNoMoreData();
                    ToastUtil.showToast("网络加载失败了！");
                }

            }
        }, AppManager.REFRESH_TIME);
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        refreshLayout.getLayout().postDelayed(new Runnable() {
            @Override
            public void run() {
                if (NetWorkHelper.isConnected(getApplicationContext())) {
                    if (listBeans.size() >= 10000) {
                        ToastUtil.showToast("数据全部加载完毕");
                        refreshLayout.finishLoadmoreWithNoMoreData();//设置之后，将不会再触发加载事件
                    } else {
                        page++;
                        initData(page);
                        refreshLayout.finishLoadmore();
                    }
                } else {
                    refreshLayout.finishLoadmore(false);
                    ToastUtil.showToast("网络加载失败了!");
                }
            }
        }, AppManager.LOAD_MORE_TIME);
    }
}
